package cn.com.inossem.operation;

import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import cn.com.inossem.config.WSConfig;
import cn.com.inossem.network.NetworkConnect;

import com.foxykeep.datadroid.exception.ConnectionException;
import com.foxykeep.datadroid.exception.CustomRequestException;
import com.foxykeep.datadroid.exception.DataException;
import com.foxykeep.datadroid.requestmanager.Request;
import com.foxykeep.datadroid.service.RequestService.Operation;

public class PackageVersionOperation implements Operation {
	public static final String PACKAGE_VERSION_OPERATION_STR = "package_version_operation_str";
	
	@Override
	public Bundle execute(Context arg0, Request request) throws ConnectionException, DataException, CustomRequestException {
		NetworkConnect connection = new NetworkConnect(WSConfig.LOCAL_URL + "userInfo/checkVersion");
		connection.setPostText("{\"phone\":\"\"}");
		String body = connection.execute();
		Log.i("PackageVersionOperation", body);
		Bundle bundle = new Bundle();
		try {
			JSONObject jsonObject = new JSONObject(body);
			int errorFlag = jsonObject.getInt("errorFlag");
			if (errorFlag == WSConfig.RESULT_SUCESS) {
				String version = (String)jsonObject.get("version");
				bundle.putString(PACKAGE_VERSION_OPERATION_STR, version);
			} 
		} catch (JSONException e) {
			Log.i("PackageVersionOperation", "" + e.getMessage());
		}
		return bundle;
	}
}
